package com.hqyj.onlineExamination.modules.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.onlineExamination.modules.account.entity.User;
import com.hqyj.onlineExamination.modules.common.entity.ResultEntity;
import com.hqyj.onlineExamination.modules.common.entity.SearchBean;
import com.hqyj.onlineExamination.modules.exam.dao.ApprovalDao;
import com.hqyj.onlineExamination.modules.exam.dao.PaperDao;
import com.hqyj.onlineExamination.modules.exam.entity.Achievement;
import com.hqyj.onlineExamination.modules.exam.entity.Approval;
import com.hqyj.onlineExamination.modules.exam.entity.Paper;
import com.hqyj.onlineExamination.modules.exam.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class ApprovalServiceImpl implements ApprovalService {
    @Autowired
    private ApprovalDao approvalDao;

    @Autowired
    private PaperDao paperDao;

    @Override
    @Transactional
    public ResultEntity<Approval> insertApproval(Integer paperId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Approval approval = new Approval();
        approval.setPaperId(paperId);
        approval.setStatus("待审批");
        approval.setUserId(user.getId());
        Approval approval1 = approvalDao.getApproval(approval);
        if (approval1 != null){
            return new ResultEntity<Approval>(ResultEntity.ResultStatus.FAILED.status,"您已经报名了该考试");
        }else {
            approvalDao.insertApproval(approval);
        }
        return new ResultEntity<Approval>(ResultEntity.ResultStatus.SUCCESS.status,"报名成功");
    }

    @Override
    public ResultEntity<Approval> getApprovals(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<Integer> paperIds = approvalDao.getApprovalsByUserId(user.getId());
        if (paperIds.size() == 0){
            return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,"您暂时无考试信息");
        }else {
            List<Paper> papers = new ArrayList<Paper>();
            for (Integer paperId : paperIds) {
                Paper paper = paperDao.getPaperById(paperId);
                if (paper != null){
                    papers.add(paper);
                }
            }
            if (papers.size() == 0){
                return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,"无考生考试试卷");
            }else {
                Approval approval = new Approval();
                approval.setPapers(papers);
                approval.setUserId(user.getId());
                return new ResultEntity<Approval>(ResultEntity.ResultStatus.SUCCESS.status,"进入考试",approval);
            }
        }
    }

    @Override
    public PageInfo<Approval> getApprovalsBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(),searchBean.getPageSize());
        return new PageInfo<Approval>(Optional
                .ofNullable(approvalDao.getApprovalsBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }

    @Override
    public PageInfo<Achievement> getGradesBySearchBean(SearchBean searchBean, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(),searchBean.getPageSize());
        return new PageInfo<Achievement>(Optional
                .ofNullable(approvalDao.getGradesBySearchBean(searchBean, userId))
                .orElse(Collections.emptyList()));
    }

    @Override
    @Transactional
    public ResultEntity<Approval> updateApprovalById(Approval approval) {
        approvalDao.updateApprovalById(approval);
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,"审批成功");
    }
}
